import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import minidraw.framework.Drawing;
import minidraw.framework.DrawingEditor;
import minidraw.standard.MiniDrawApplication;

public class PuzzleTest {
	
	private static Drawing drawing;
	private static PuzzleGame game = null;

	public static void main(String[] args) {
		prompIntroScreen();
	}
	
	public static void prompIntroScreen() {
		final JFrame frame = new JFrame("PuzzleGame");
		frame.setLayout(new BorderLayout());
		JPanel top = new JPanel();
		JPanel bottom = new JPanel();
		
		top.setLayout(new GridLayout(2,2));
		top.add(new JLabel("Size:"));
		final SpinnerNumberModel size = new SpinnerNumberModel(2,2,5,1);
		top.add(new JSpinner(size));
		top.add(new JLabel("Difficulty:"));
		final SpinnerNumberModel diff = new SpinnerNumberModel(0,0,100000000,1);
		top.add(new JSpinner(diff));
		frame.add(top, BorderLayout.NORTH);
		
		JButton button = new JButton("Ok");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int GAMESIZE = size.getNumber().intValue();
				int difficulty = diff.getNumber().intValue();
				promptGame(GAMESIZE, difficulty);
				frame.setVisible(false);
			}
		});
		bottom.add(button, BorderLayout.CENTER);
		frame.add(bottom, BorderLayout.SOUTH);		
		
		frame.setVisible(true);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}
	
	public static void promptGame(int GAMESIZE, int difficulty) {
		game = new PuzzleGame(GAMESIZE);
		game.scramble(difficulty); //THIS SCRAMBLE-ALGORITHM IS FUCKED!
		DrawingEditor editor = 
			new MiniDrawApplication( "PuzzleTest",
					new PuzzleFactory(game.getGamesize()*100) );
		editor.open();
		editor.setTool( new SwitchTool(editor, game) );
		
		game.addGameListener(new GameListener() {
			public void gameChanged(GameEvent e) {
				if(e.solved()) {
					promptSolvedScreen(e.steps());
				}
			}
		});

		drawing = editor.drawing();
		addFieldFigures(game);
	}
	
	public static void promptSolvedScreen(int steps) {
		JFrame frame = new JFrame("Game solved");
		frame.add(new JLabel("Game solved in " + steps + " steps."), BorderLayout.NORTH);
		JButton ok = new JButton("Ok");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		frame.add(ok, BorderLayout.SOUTH);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public static void addFieldFigures(PuzzleGame game) {
		Field f;
		for(int i = 0; i < game.getGamesize(); i++) {
			for(int j = 0; j < game.getGamesize(); j++) {
				f = game.getFieldAt(new Position(j+1, i+1));
				drawing.add(new FieldFigure(f, new Point(i*100, j*100)));
			}
		}
	}
}
